<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Abstract Class for all Back-End Controllers
 *
 * @author	kimon.web.designs - Application Framework
 * @package 	CMS
 * @subpackage 	Admin_Controller
 * @category 	Framework
 * @copyright	Copyright (c) 2010, kimon.web.design
 */

class Admin_Controller extends MY_Controller
{

    /**
     * Constructor method
     * @access public
     * @return void
     */
    public function  __construct() {
        parent::__construct();

        $this->prepareTemplate();
    }

    /**
     * set the Master Template of the Controller
     *
     * @access public
     * @return string
     */
    protected function __defineTemplateView()
    {
        $this->template_name = 'admin';
    }

    /**
     * set if the Controller needs Authentication
     *
     * @access public
     * @return void
     */
    protected function __defineAuthentication()
    {
        $this->needs_authentication = TRUE;
    }


    private function getSubMenu($group)
    {
        if (!is_array($group))
        {
            return;
        }
        array_multisort($group, SORT_ASC);
        $html = '';
        foreach ($group as $item)
        {
            $html .= '<li>' . anchor('/admin'.$item['url'], $item['name']);
            if (is_array($item) && key_exists('items', $item))
            {
                $html .=  '<ul>' . $this->getSubMenu($item['items']) . '</ul>';
            }
            $html .= '</li>';
        }

        return $html;
    }

    /**
     * Returns the Administrator Menu
     * @acess private
     * @return void
     */
    private function getMenuHtml()
    {
        $this->load->config('admin');
        $kwd_modules = $this->config->item('kwd.modules');

        $menu_all = array();
        foreach ($kwd_modules as $module_dir)
        {
            $lib_name = $module_dir . '/' . ucfirst($module_dir) . '_admin_menu';
            $filename = APPPATH . 'modules' . DIRECTORY_SEPARATOR . $module_dir . DIRECTORY_SEPARATOR . 'libraries' . DIRECTORY_SEPARATOR . ucfirst($module_dir) . '_admin_menu' . EXT;

            if (file_exists($filename))
            {
                $this->load->library($lib_name, '', 'admin_menu');
                $menu = $this->admin_menu->getMenu();
                $menu_all = array_merge_recursive($menu_all, $menu);
            }
        }
        return $this->getSubMenu($menu_all);
    }

    /**
     *
     */
    protected function prepareTemplate()
    {
        $menuHtml  = $this->getMenuHtml();
        $this->template->write('sidemenu', $menuHtml);
    }

}

/* End of file Admin_Controller.php */
/* Location: ./application/core/Admin_Controller.php */